package com.tuozixuan;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.github.houbb.opencc4j.util.ZhConverterUtil;

/**
 * 
 * 檢測項目中的簡體字<br>
 * 
 * @author Weng.weng
 * @version [版本号, Dec 13, 2021]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class Application {
    
    public static void main(String[] args) throws IOException {
        
        Properties properties = getPropertiesFile();
        String searchPath = (String)properties.get("searchPath");
        String[] fileExtensions = getArrayValueByComma(properties, "fileExtensions");
        String[] ignorePrefixs = getArrayValueByComma(properties, "ignorePrefixs");
        String outputFile = (String)properties.get("outputFile");
        
        int total = 0;
        List<String> allResultList = new ArrayList<>();
        File basePath = new File(searchPath);
        Collection<File> fileList = FileUtils.listFiles(basePath, fileExtensions, true);
        for (File file : fileList) {
            
            String filePath = file.getAbsolutePath();
            filePath = StringUtils.removeStart(filePath, searchPath);
            if (StringUtils.contains(filePath, "\\target\\")) {
                continue;
            }
            
            List<String> resultList = getCheckResultList(file, ignorePrefixs);
            if (!resultList.isEmpty()) {
                allResultList.add(String.format("%s: %d \r\n\r\n", filePath, resultList.size()));
                allResultList.addAll(resultList);
                allResultList.add("==========================================================================");
                total += resultList.size();
            }
        }

        allResultList.add(String.format("total: %d", total));
        allResultList.stream().forEach(System.out::println);
        
        if (StringUtils.isNotBlank(outputFile)) {
            FileUtils.writeLines(new File(outputFile), allResultList);
        }
    }
    
    /**
     * 
     * 獲取數組類型的值<br>
     * 以英文逗號分隔
     *
     * @param properties 屬性對象
     * @param key Key
     * @return String[] 數組類型的值
     * @version [版本号, Dec 14, 2021]
     * @author Weng.weng
     */
    private static String[] getArrayValueByComma(Properties properties, String key) {
        String value = (String)properties.get(key);
        String[] valueItems = value.split(" *, *");
        return valueItems;
    }
    
    /**
     * 
     * 獲取存在簡體字的行列表<br>
     *
     * @param file 需檢測的文件
     * @param ignorePrefixs 不需要檢測的行的前綴
     * @return List<String> 存在簡體字的行列表
     * @version [版本号, Dec 14, 2021]
     * @author Weng.weng
     */
    private static List<String> getCheckResultList(File file, String[] ignorePrefixs) throws IOException {
        List<String> resultList = new ArrayList<>();
        List<String> lines = FileUtils.readLines(file, "UTF-8");
        for (String line : lines) {
            if (StringUtils.isNotBlank(line)) {
                
                line = line.trim();
                if (StringUtils.startsWithAny(line, ignorePrefixs)) {
                    continue;
                }
                
                String traditionalLine = ZhConverterUtil.toTraditional(line);
                if (!traditionalLine.equals(line)) {
                    resultList.add(line + "  -->  " + traditionalLine);
                }
            }
        }
        return resultList;
    }
    
    /**
     * 
     * 從配置文件中獲取配置信息<br>
     *
     * @return Properties [返回类型说明]
     * @version [版本号, Dec 14, 2021]
     * @author Weng.weng
     */
    private static Properties getPropertiesFile() throws IOException {  
        
        Properties prop = new Properties();  
        try (InputStream is = Application.class.getClassLoader().getResourceAsStream("config.properties")) {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return prop;  
  
    }  
}
